unit SmallStruct.Action.TSSAction;

interface

uses
  Classes,
  SmallStruct.Core,
  SmallStruct.Core.TSSRuntimeObject,
  SmallStruct.Core.TSSObjectContainer,
  SmallStruct.Core.TSSObjectContext,
  SmallStruct.Action,
  SmallStruct.Controller,
  SmallStruct.Controller.TSSController,
  SmallStruct.View,
  SmallStruct.View.TSSView;

type
  TSSActionContext = class(TSSObjectContext, ISSActionContext)
  private
    _BOContainer, _Resource: ISSObjectContainer;
    _Controller: ISSController;
    _RequestView, _ResponseView: ISSView;
  public
    procedure SetController(AController: ISSController);
    procedure SetRequestView(ARequestView: ISSView);
    procedure SetResponseView(AResponseView: ISSView);
  public
    function GetBOContainer: ISSObjectContainer;
    function GetController: ISSController;
    function GetRequestView: ISSView;
    function GetResource: ISSObjectContainer;
    function GetResponseView: ISSView;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

  TSSAction = class(TSSRuntimeObject, ISSAction)
  private
    function Perform: Boolean;
  protected
    function DoPerform(ActionContext: ISSActionContext): Boolean; virtual; abstract;
  end;

implementation

uses SysUtils,
  SmallStruct.Error;

{ TSSAction }

function TSSAction.Perform: Boolean;
begin
  try
    Result := DoPerform(GetContext as ISSActionContext);
  except
    on E: Exception do
    begin
      Result := False;
      raise ESSOperationException.Create(ClassName, 'Perform', E.Message);
    end;
  end;
end;

{ TSSActionContext }

function TSSActionContext.GetResponseView: ISSView;
begin
  Result := _ResponseView;
end;

function TSSActionContext.GetResource: ISSObjectContainer;
begin
  Result := _Resource;
end;

procedure TSSActionContext.SetRequestView(ARequestView: ISSView);
begin
  _RequestView := ARequestView.GetObjectInstance as TSSView;
end;

function TSSActionContext.GetBOContainer: ISSObjectContainer;
begin
  Result := _BOContainer;
end;

function TSSActionContext.GetController: ISSController;
begin
  Result := _Controller;
end;

procedure TSSActionContext.SetResponseView(AResponseView: ISSView);
begin
  _ResponseView := AResponseView.GetObjectInstance as TSSView;
end;

procedure TSSActionContext.SetController(AController: ISSController);
begin
  _Controller := AController.GetObjectInstance as TSSController;
end;

function TSSActionContext.GetRequestView: ISSView;
begin
  Result := _RequestView;
end;

constructor TSSActionContext.Create(AOwner: TComponent);
begin
  inherited;
  _Resource := TSSObjectContainer.Create(Self);
  _BOContainer := TSSObjectContainer.Create(Self);
end;

destructor TSSActionContext.Destroy;
begin
  _Resource.ReleaseObjectInstance;
  _BOContainer.ReleaseObjectInstance;
  inherited;
end;

end.
